package com.pomr.service.common.impl;

import com.pomr.dao.base.BaseDao;
import com.pomr.model.sys.Agency;
import com.pomr.model.sys.Role;
import com.pomr.model.sys.User;
import com.pomr.service.common.CreateUserService;
import com.pomr.util.common.ChineseCharToEnUtil;
import com.pomr.util.common.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @作者：Administrator
 * @时间：22:51 2018/9/10 0010
 * @用途：
 **/
@Service
public class CreateUserServiceImpl implements CreateUserService {
    @Autowired
    @Qualifier("baseDaoImpl")
    private BaseDao baseDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData createUser() throws Exception {
        String hql = "from Agency where identifier like '3702__'";
        List<Agency> agencies = baseDao.findByHql(hql);
        System.out.println("机构数目："+agencies.size());
        for(Agency agency:agencies){
            String name = agency.getName();
            ChineseCharToEnUtil cte = new ChineseCharToEnUtil();
            String szm = cte.getAllFirstLetter(name).replace("崂","l").
                    replace("庵","a").replace("汶","w");//机构名称首字母作为用户名
            //高级用户
            User user = new User();
            user.setUname(szm);//用户名
            user.setPassword("MTIzNDU2");//密码 默认123456
            user.setName(agency.getName()+"高级");
            user.setAgency(agency);
            user.setAgencyName(agency.getName());
            user.setStatus("0");
            Role role = new Role();
            role.setId(2);
            user.setRole(role);
            user.setCreateTime(new Date());
            user.setRoleName("高级管理员");
            if(!baseDao.add(user)){
                throw new Exception("保存"+agency.getName()+"高级管理员失败");
            }
            //普通用户
            User user1 = new User();
            user1.setUname(szm+"p");//用户名
            user1.setPassword("MTIzNDU2");//密码 默认123456
            user1.setName(agency.getName()+"普通");
            user1.setAgency(agency);
            user1.setAgencyName(agency.getName());
            user1.setStatus("0");
            Role role1 = new Role();
            role1.setId(3);
            user1.setRole(role1);
            user1.setCreateTime(new Date());
            user1.setRoleName("普通管理员");
            if(!baseDao.add(user1)){
                throw new Exception("保存"+agency.getName()+"高级管理员失败");
            }
        }
        return ResponseData.ok();
    }
}
